package cn.yunhe.util;

import cn.yunhe.entity.Admin;
import cn.yunhe.server.AdminServer;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

public class MyRealm extends AuthorizingRealm {

    /**
     * 权限验证
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 登录验证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = token.getPrincipal().toString();
        AdminServer adminServer = new AdminServer();
        Admin admin = new Admin();
        admin.setAname(username);
        Admin newAdmin = adminServer.Login(admin);
        if (newAdmin != null) {
            AuthenticationInfo authorizationInfo = new SimpleAuthenticationInfo(newAdmin.getAname(), newAdmin.getApass(), "1");
            return authorizationInfo;
        } else {
            return null;
        }
    }
}
